package com.shiningstars.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

import com.shiningstars.R;
import com.shiningstars.general.Config;
import com.shiningstars.general.Constants;
import com.shiningstars.general.Messages;
import com.shiningstars.general.Utilities;
import com.shiningstars.tools.ExceptionHandler;
import com.shiningstars.valuebeans.Feedback;

public class ViewFeedback2Activity extends Activity implements Constants, Messages, Config {

	Feedback feedback;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_teacher_feedback_2);

			Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
			SplashPageActivity.showSplash = false;

			getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

			RadioButton radioPathshalaTimeYes = (RadioButton) findViewById(R.id.tf2RadioButton1);
			RadioButton radioPathshalaTimeNo = (RadioButton) findViewById(R.id.tf2RadioButton2);
			EditText editTextMinsDelay = (EditText) findViewById(R.id.tf2EditText1);
			RadioButton radioPrayersYes = (RadioButton) findViewById(R.id.tf2RadioButton3);
			RadioButton radioPrayersNo = (RadioButton) findViewById(R.id.tf2RadioButton4);
			RadioButton radioGuruvandanYes = (RadioButton) findViewById(R.id.tf2RadioButton5);
			RadioButton radioGuruvandanNo = (RadioButton) findViewById(R.id.tf2RadioButton6);
			RadioButton radioAttendanceYes = (RadioButton) findViewById(R.id.tf2RadioButton7);
			RadioButton radioAttendanceNo = (RadioButton) findViewById(R.id.tf2RadioButton8);
			EditText editTextKidsPresent = (EditText) findViewById(R.id.tf2EditText2);
			RadioButton radioRevisionYes = (RadioButton) findViewById(R.id.tf2RadioButton9);
			RadioButton radioRevisionNo = (RadioButton) findViewById(R.id.tf2RadioButton10);
			RatingBar ratingBarRevisionResponse = (RatingBar) findViewById(R.id.tf2RatingBar1);
			EditText editTextPage2Comments = (EditText) findViewById(R.id.tf2EditText3);

			ratingBarRevisionResponse.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

				@Override
				public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
					TextView textViewRevisionResponse = (TextView) findViewById(R.id.tf2TextView1);
					Utilities.setRatingDescription(textViewRevisionResponse, rating);
				}
			});

			if (null != getIntent()) {
				if (null != getIntent().getExtras()) {
					if (null != getIntent().getExtras().get(FEEDBACK)) {
						feedback = (Feedback) getIntent().getExtras().get(FEEDBACK);

						if (YES.equals(feedback.getQuestion12())) {
							radioPathshalaTimeYes.setChecked(true);
							editTextMinsDelay.setText(BLANK);
						} else if (NO.equals(feedback.getQuestion12())) {
							radioPathshalaTimeNo.setChecked(true);
							editTextMinsDelay.setText(feedback.getQuestion13());
						}

						if (YES.equals(feedback.getQuestion14())) {
							radioPrayersYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion14())) {
							radioPrayersNo.setChecked(true);
						}

						if (YES.equals(feedback.getQuestion15())) {
							radioGuruvandanYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion15())) {
							radioGuruvandanNo.setChecked(true);
						}

						if (YES.equals(feedback.getQuestion16())) {
							radioAttendanceYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion16())) {
							radioAttendanceNo.setChecked(true);
						}

						editTextKidsPresent.setText(feedback.getQuestion17());

						if (YES.equals(feedback.getQuestion18())) {
							radioRevisionYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion18())) {
							radioRevisionNo.setChecked(true);
						}

						if (!Utilities.isBlankString(feedback.getQuestion19())) {
							float rating = Float.parseFloat(feedback.getQuestion19());
							ratingBarRevisionResponse.setRating(rating);
						}

						editTextPage2Comments.setText(feedback.getQuestion20());

						radioPathshalaTimeYes.setEnabled(false);
						editTextMinsDelay.setEnabled(false);
						radioPathshalaTimeNo.setEnabled(false);
						radioPrayersYes.setEnabled(false);
						radioPrayersNo.setEnabled(false);
						radioGuruvandanYes.setEnabled(false);
						radioGuruvandanNo.setEnabled(false);
						radioAttendanceYes.setEnabled(false);
						radioAttendanceNo.setEnabled(false);
						editTextKidsPresent.setEnabled(false);
						radioRevisionYes.setEnabled(false);
						radioRevisionNo.setEnabled(false);
						ratingBarRevisionResponse.setEnabled(false);
						editTextPage2Comments.setEnabled(false);
					}
				}
			}
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), ViewFeedback2Activity.class.getName(), e);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		try {
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.feedback_menu, menu);
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), ViewFeedback2Activity.class.getName(), e);
		}

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		try {
			Intent i = null;

			if (BACK.equals(item.getTitle())) {
				i = new Intent(ViewFeedback2Activity.this, ViewFeedback1Activity.class);
			} else if (NEXT.equals(item.getTitle())) {
				i = new Intent(ViewFeedback2Activity.this, ViewFeedback3Activity.class);
			}

			i.putExtra(FEEDBACK, feedback);
			startActivity(i);
			finish();
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), ViewFeedback2Activity.class.getName(), e);
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		try {
			AlertDialog.Builder builder = new AlertDialog.Builder(ViewFeedback2Activity.this);

			builder.setMessage(FINISHED_VIEW_FEEDBACK);
			builder.setCancelable(false);
			builder.setPositiveButton(OK, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Intent i = null;

					String currentRole = Utilities.getSharedPreference(getApplicationContext(), CURRENT_ROLE);
					if (ADMIN.equals(currentRole)) {
						i = new Intent(ViewFeedback2Activity.this, AdminActivity.class);
					} else if (TEACHER.equals(currentRole)) {
						i = new Intent(ViewFeedback2Activity.this, MyFeedbackActivity.class);
					}

					startActivity(i);
					finish();
				}
			});
			builder.setNegativeButton(CANCEL, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {

				}
			});

			AlertDialog alert = builder.create();
			alert.show();
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), ViewFeedback2Activity.class.getName(), e);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		SplashPageActivity.showSplash = true;
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (SplashPageActivity.showSplash) {
			LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
			View popupView = layoutInflater.inflate(R.layout.activity_splash_page, null);
			final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT, true);

			findViewById(R.id.tf2EditText1).post(new Runnable() {
				public void run() {
					popupWindow.showAtLocation(findViewById(R.id.tf2EditText1), Gravity.CENTER, 0, 0);
				}
			});

			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					if (null != popupWindow && popupWindow.isShowing()) {
						popupWindow.dismiss();
					}
				}
			}, SPLASH_TIME_OUT);

			SplashPageActivity.showSplash = false;
		}
	}
}